﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{

    public class UIFieldRunMapChangeLine : MonoBehaviour
    {
        public void Exit()
        {
            foreach(var obj in lines)
            {
                GameObject.Destroy(obj);
            }
            lines.Clear();
            Gird.Reposition();
            gameObject.SetActive(false);
        }

        public void Init()
        {
            StaticData.lineInfos.Clear();
            var room = Room.RoomManager.currentRoom as Room.RoomField;
            var netMgr = room.netManager as Room.RoomNetField;
            netMgr.SendGetLineInfo();
        }

        public void Refresh()
        {
            gameObject.SetActive(true);

            foreach (var obj in lines)
            {
                GameObject.Destroy(obj);
            }
            lines.Clear();
            Gird.Reposition();

            currentLine.text = StaticData.currLine+"";

            StartCoroutine(_Refresh());
        }

        IEnumerator _Refresh()
        {
            int i = 0;

            StaticData.lineInfos.Sort((lh, rh) => { return lh.line - rh.line; });

            foreach (var line in StaticData.lineInfos)
            {
#if UNITY_EDITOR
                Debug.Log("[LINE INFO] state:" + line.state);
#endif
                if (line.state == 1 || line.state == 2 || (line.state == 3 && line.line <=10 ))
                {
                    var obj = Instantiate(template);
                    obj.transform.SetParent(Gird.transform);
                    obj.transform.localScale = Vector3.one;
                    obj.SetActive(true);
                    obj.GetComponent<UIFieldRunMapLineInfo>().Init(line, this);
                    lines.Add(obj);
                }
                if (i++ % 2 == 0)
                    yield return null;
                Gird.Reposition();
            }
        }


        public GameObject template;
        public UIGrid Gird;
        public List<GameObject> lines = new List<GameObject>();
        public UILabel currentLine;
        
        void Awake()
        {
            template.SetActive(false);
        }

        public void Ok()
        {
            if (StaticData.lineInfo.line != StaticData.currLine)
            {

                var room = Room.RoomManager.currentRoom as Room.RoomField;
                //var self = room.charactorManager.GetSelfCharactor();
                var netMgr = room.netManager as Room.RoomNetField;
                netMgr.SendTransferLine(StaticData.currLine);

                /*
                if (self != null)
                {
                    var dict = Dict.Blo.DictFieldBlo.GetOne(StaticData.fieldId).Resurrection;
                    var sss = dict.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
                    var pos = new Vector3(float.Parse(sss[0]), float.Parse(sss[1]), float.Parse(sss[2]));
                    var ai = self.ai as Room.RCAISelf;
                    ai.AutoGoto(pos, () =>
                    {
                        
                    });
                }*/
            }

            gameObject.SetActive(false);
           
        }
    }
}